<?php
//рус
include_once (_CORE_PATH . "module/active_module.php");
class module_admin_http extends active_module_class
{
    public function Init ()
    {
    	parent::Init();
        return $this;
    }

    public function CheckAccessWithUserGranted(controller_prototype_class $ctrlObj, $basicActionName)
    {
		if ($this->GetCtrlCollection()->default->IsRightRefferer())
		{
			return $ctrlObj->CheckAccessWithUserGranted($basicActionName);			  	
		}
		return false;
    }  

    public function Auth()
    {
    	$user = $this->GetUser();
        $user->Reset();    	
    	
    	include_once(_APP_PATH. "business/users/users_mapper.php");
    	$usersMapper = UsersMapper::GetInstance($user);
    	$libs = $this->GetLibCollection();
        
        $foundUser = false;
        
        if ($this->GetRequest()->getPost('logme')) 
        {
        	$userName = trim($this->GetRequest()->getPost('site_username'));
        	$password = $this->GetRequest()->getPost('site_passwd');
        	$remember = (int)$this->GetRequest()->getPost('site_rememberme');
        	
            $foundUser = $usersMapper->GetByLoginPassword($userName, $password);
			if ($foundUser && $foundUser->GetBlocked()) $foundUser = false;
            
			if ($foundUser || $libs->session->IsSession()) 
	        {
	            $libs->session->start((bool)$foundUser);
	            
	            $sessionUser = array();
	            if ($foundUser) 
	            {
	            	if ($remember) $libs->session->SetExpireDate(3600 * 24 * 30);
	            	$sessionUser = array('is_logged' => true, 'user_id' => $foundUser->GetUserId());
	            }
	            
	            $libs->session->SetValue('user', $sessionUser);
	        }             
            
            if (!$foundUser) 
            {
                $user->SetUserName($userName);
                $user->SetError('Неправильный логин/пароль');
                
                $this->AfterFailedLogin();
                
                return $this;
            } 

            $this->AfterSuccessLogin();
        } 
        else if (!$libs->session->IsSession()) 
        {
            return $this;
        } 
        else if ($this->GetRequest()->getPost('logout')) 
        {
	        $libs->session->start();
	        $libs->session->stop();
	        $this->AfterSuccessLogout();
	        
            return $this;
        }
        
        //запускаем сессию, если ещё не запущена
        $libs->session->start();
        $sessionUser = $libs->session->GetValue('user');
        
        if (!$sessionUser['is_logged']) 
        {
            return $this;
        }

        if (!$foundUser) 
        {
        	if (0 === $sessionUser['user_id']) $foundUser = $usersMapper->GetSuperUser(); 
        	else $foundUser = $usersMapper->GetById($sessionUser['user_id']);      	        
        	
        	if ($foundUser && $foundUser->GetBlocked()) return $this; 
        }

        $user->Set(true, $foundUser->GetUserId(), $foundUser->GetUserName(), $foundUser->GetUserGroup(), $foundUser->GetGrantedGroups());
        $user->SetDetails(array('groupName' => $foundUser->GetGroupName() ? $foundUser->GetGroupName() : 'bots'  , 'email' => $foundUser->GetEmail() , 'sessionId' => $libs->session->get_session_id()));
    	$user->SetAvatar($foundUser->GetAvatar());    	
    	
    	return $this;
    }
    
    protected function AfterFailedLogin()
    {
  	
    }     
    
    protected function AfterSuccessLogin()
    {
    	
    }    
    
    protected function AfterSuccessLogout()
    {
    	
    }
}